package com.hutao.example.wardemo.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlUtil {

    public static String readNode(String fileName, String node) {
        String value = "";
        // 1. 创建SAXReader对象
        SAXReader reader = new SAXReader();

        try {
            // 2. 读取XML文件，获取Document对象
            Document document = reader.read(XmlUtil.class.getClassLoader().getResourceAsStream(fileName));

            // 3. 获取根节点（<books>）
            Element rootElement = document.getRootElement();

            // 4. 遍历根节点下的所有子节点（<book>）
            Element element =  rootElement.element(node);
            if (element != null) {
               value = element.getText();
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return value;
    }
}
